# -*- coding: utf-8 -*-
# @Time : 2022/4/23 16:23
# @Author : cheney
# @File : log.py
# @Software: PyCharm
# @Site: www.cheney.cc
"""
log模块，输出调试信息到指定的文件下
"""
from datetime import datetime
import traceback
import os


class log:
    path = None
    log_name = None
    print_tb = None

    def __init__(self, path: str = "./", log_name: str = "log", print_tb: bool = False):
        self.path = path
        self.log_name = log_name
        dt = str(datetime.now())  # 获取当前时间
        self.log_name = log_name + "_" + dt[0:19].replace(" ", "_") + ".txt"
        self.print_tb = print_tb

        pass

    def debug(self, content: str) -> bool:
        """
        :param print_tb:
        :param content: 要写到日志里面的内容
        :param path: 写日志的路径 默认为当前目录
        :param log_name: 日志文件名 默认为log.txt
        :return: 是否写成功
        """
        if not os.path.isdir(self.path):
            a = os.getcwd()
            os.mkdir(a + '//' + self.path)
            # 这里进行错误处理 !!! 待完善
            # return False
        dt = str(datetime.now())  # 获取当前时间
        f = open(self.path + self.log_name, 'a')  # 追加写
        f.write("\n")

        # caller_filename = traceback.extract_stack()[-2][0]
        f.write(dt)
        f.write("\n")
        caller_filename = ""
        if self.print_tb:
            for i in traceback.extract_stack():  # 获得调用log的文件名以及对应的行
                # caller_filename += str(i)
                f.write(str(i))
                f.write("\n")
        f.write(content + "\n" + "_" * 50 + "\n")
        f.close()

        return True


if __name__ == '__main__':
    logger = log()
    for i in range(10):
        status = logger.debug("这是一个测试信息，测试输出log的功能")
        if status:
            print("写日志成功")
        else:
            print("写日志失败")
        # cbkajbcabcabokc
